﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.ScheduleJob.Engine;
using T.FAS.Runtime.ScheduleJob.Manager;
using T.FAS.Runtime.ScheduleJob.SPI;

namespace T.FAS.Runtime.ScheduleJob.Service
{
    public class ScheduleJobSubscriptionService : IScheduleJobSubscriptionService
    {
        private readonly static object lockObj = new object();

        private static ScheduleJobSubscriptionService _service = null;

        private ScheduleJobSubscriptionManager subManager = null;
        private IScheduleJobService jobService = null;
        private ScheduleJobInstanceManager sJobManager = null;
        public static IScheduleJobSubscriptionService GetInstance()
        {
            if (_service == null)
            {
                lock (lockObj)
                {
                    if (_service == null)
                        _service = new ScheduleJobSubscriptionService();
                }
            }
            return _service;
        }
        private ScheduleJobSubscriptionService()
        {
            subManager = ScheduleJobSubscriptionManager.GetInstance();
            jobService = ScheduleJobService.GetInstance();
            sJobManager = ScheduleJobInstanceManager.GetInstance();
        }
        /// <summary>
        /// 创建计划任务订阅，由于一个订阅会指定多个执行频率，所以一个订阅会对应多个计划任务实例
        /// </summary>
        /// <param name="subscription"></param>
        /// <returns></returns>
        public string CreateScheduleJobSubscription(ScheduleJobSubscription subscription)
        {
            subscription.JSContentParams = subscription.JSContentParams.Replace("\\\"Value\\\":\\\"@我\\\"", $"\\\"Value\\\":[\\\"{UserUtil.GetCurrentUserID()}\\\",\\\"{UserUtil.GetCurrentUserName()}\\\"]");
            var subId = subManager.CreateScheduleJobSubscription(subscription);
            var instances = ScheduleJobWrapper.WrapScheduleJobSubscription(subscription);
            instances.ForEach(i => jobService.StartJob(i));
            return subId;
        }

        public ScheduleJobSubscription GetScheduleJobSubscriptionById(string subscriptionId)
        {
            return subManager.GetScheduleJobSubscriptionById(subscriptionId);
        }

        public void CancleScheduleJobSubscription(string subscriptionId)
        {
            var filter = new UserDefineQueryFilter();
            filter.Add("ScheduleJobSubscriptionID", subscriptionId);
            var instances = sJobManager.GetScheduleJobInstances(filter);
            if (instances != null && instances.Count > 0)
            {
                instances.ForEach(i =>
                {
                    jobService.DeleteJob(i.InstanceID);
                });
            }
            this.DeleteScheduleJobSubscription(subscriptionId);
        }

        public List<ScheduleJobSubscription> GetScheduleJobSubscriptionsByFilter(UserDefineQueryFilter filter)
        {
            return subManager.GetScheduleJobSubscriptionsByFilter(filter);
        }
        public void DeleteScheduleJobSubscription(string subscriptionId)
        {
            subManager.DeleteScheduleJobSubscription(subscriptionId);
        }


        #region 私有方法

        #endregion
    }
}
